var Mul_dm = (function(){ 
	var mul = document.getElementById("mul-downmenu");
	var _siback = null;
	var _maincall = null;
	var _subcall = null;
	var _type="*";
	var _subcity = "*";
	var _topcity = "*";
	var _order = "*";
	var page=1;
	
	var downmenu = function(){
		
	}
	
	downmenu.prototype.init = function(switchback,maincallback,subcallback){
		eventinit();
		_siback = switchback;
		_maincall = maincallback; 
		_subcall = subcallback;
	}
	
	var eventinit = function(){
		var head = $(".downmenu-head");
		var list = $(".mul-downmenu-list");
		var crt_h = ""; 
		var posting = false;
		list.click(function(){
			headhide();
		});
		head.click(function(){
			var tid = $(this).attr("data-id");
			if(list.css("opacity")==1){
				if(crt_h==$(this).text()){
					headhide();
				}else {
					if(tid==3){
						list.empty();
						list.append(orderh);
						mainevent();
						crt_h = $(this).text();
						return;
					}
					if(!posting){
						posting = true;
						$.post(
							hact(tid),"",
							function(data){
								addhtml(data,list,tid);
								posting = false;
							},"json"
						);
					}
				}
			}else {
				headshow();
				if(tid==3){
					list.empty();
					list.append(orderh);
					mainevent();
					crt_h = $(this).text();
					return;
				}
				if(!posting){
					posting = true;
					$.post(
						hact(tid),"",
						function(data){
							addhtml(data,list,tid);
							posting = false;
						},"json"
					);
				}
			}
			crt_h = $(this).text();
		});
	}
	//一级分类
	var mainevent =function(tid){
		var mainlist = $(".main-list");
		var mainitem = $(".list-item");
		var sublist = $(".sub-list");
		mainitem.click(function(event){
			page= 1;
			mainlist.children(".select").removeClass("select");
			$(this).addClass("select");
			var dataid = $(this).attr("data-id");
			if(tid=="2"){
				if(dataid!="*"){
					_topcity = dataid;
					sublist.empty();
					$.post(
						"/HAction/subcity",{"id":$(this).attr("data-id")},function(data){
							if(data.status==1){
								var dt = data.info;
								var html = '<div data-id="*" class="list-item"><span>全部城市</span></div>';
								for (var i =0;i<dt.length;++i) {
									html +='<div data-id="'+dt[i]["id"]+'" class="list-item"><span>'+dt[i]["pvalue"]+'</span></div>';
								}
								sublist.append(html);
								subevent();
							}
						},"json"
					);
				}else{
					_topcity = "*";
					$(".hcity").text($(this).text());
					headhide();
					itemselc();
				}
			}else{
				if(tid=="1"){
					_type = dataid;
					$(".htype").text($(this).text());
				}
				else{
					_order=dataid;
					$(".horder").text($(this).text());
				}
				headhide();
				itemselc();
			}
			event.stopPropagation();
		});
	}
	//二级分类
	var subevent =function(){
		var sublist = $(".sub-list .list-item");
		sublist.click(function(){
			var subid = $(this).attr("data-id");
			_subcity =subid;
			if(subid=="*")$(".hcity").text($(".main-list .select").text());
			else $(".hcity").text($(this).text());
			itemselc();
			headhide();
		})
	}
	var showpos = $(".showpos-box");
	downmenu.prototype.itemselc = function(back){
		itemselc(back);
	}
	var itemselc =function(back){
//		console.log("1:"+_type);
//		console.log("2:"+_topcity);
//		console.log("3:"+_subcity);
//		console.log("4:"+_order);
		showpos.empty();
		_load.show();
		$.post(
			"/HAction/itemselc",{
				"type":_type,
				"topcity":_topcity,
				"subcity":_subcity,
				"order":_order,
				"page":page
			},function(data){
				_load.hide();
				if(data.status==1){
					var dt =data.info;
					var html = '';
					console.log(dt);
					for(var i=0;i<dt.length;++i){
						if(dt[i]["is_roll"]==2)continue;
						if(dt[i]["ad_id"]!=null){
							if(dt[i]["status"]!=3)continue;
							html +='<a href="/HGrid/merchant/id/'+dt[i]["ad_id"]+'" class="showpos-item ">'+
								'<div class="item-box">'+
									'<img src="'+dt[i]["icon"]+'"/>'+
									'<div class="item-name"><span>'+dt[i]["link_name"]+'</span></div>'+
								'</div></a>';
						}else{
							html +='<a href="/HGrid/gridorder/order/'+dt[i]["stand_id"]+'" class="showpos-item noad">'+
								'<div class="item-box">'+
									'<img src="/themes/simplebootx_mobile/Public/img/gridad.jpg"/>'+
									'<div class="item-name">'+
										'<span>#'+dt[i]["id"]+'展位</span>'+
										'<span>('+dt[i]["change_day"]+'金钱/天)</span>'+
									'</div></div></a>';
						}
					}
					showpos.append(html);
				}
				if(back!=null)back(data);
			},"json"
		);
	}
	
	var orderh = '<div class="main-list">'+
	'<div data-id="create_time desc" class="list-item"><span>默认排序</span></div>'+
	'<div data-id="create_time desc" class="list-item"><span>最新</span></div>'+
	'<div data-id="click_count desc" class="list-item"><span>最热门</span></div>'+
	'<div>';
	var addhtml =function(data,list,tid){
		if(data.status==1){
			list.empty();
			var dt = data.info;
			var html = '<div class="main-list"><div data-id="*"  class="list-item"><span>全部</span></div>';
			for(var i=0;i<dt.length;++i){
				html+='<div data-id="'+dt[i]["id"]+'" class="list-item"><span>'+dt[i]["pvalue"]+'</span></div>';
			}
			html+='<div>';
			list.append(html);
			if(tid==2)list.append('<div class="sub-list"></div>');
			mainevent(tid);
		}
	}
	var hact= function(head){
		var act ="";
		switch(head){
			case "1": act="/HAction/toptype";break;
			case "2": act="/HAction/topcity";break;
		}
		return act;
	}
	
	var headshow =function(obj){
	  $("body").css({"overflow-y":"hidden"});
		$(".mul-downmenu-list").css("opacity",1);
		$(".mul-downmenu-list").show();
	}
	
	var headhide =function(obj){
	  $("body").css({"overflow-y":"visible","height":"auto"});
		var mulli = $(".mul-downmenu-list");
		mulli.hide();
		mulli.css("opacity",0);
		mulli.removeClass("select");
	}
	
	var switchhead = function(){
		console.log("切换");
	}
	
	var removecls =function(obj,cls){
		if(hasClass(obj,cls)){
			var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
			obj.className = obj.className.replace(reg,"");
		}
	}
	
	var addcls =function(obj,cls){
		obj.className += " "+cls;
	}
	
	var  hasClass =function(obj, cls) {
        return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    }
	
	return downmenu;
})();

